package com.work.common.util;

import com.work.common.base.WaterMarkImage;
import org.apache.commons.lang3.ObjectUtils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

public class ImageUtil {
    private static final int WATERMARK_FONT_SIZE = 20;
    private static final Color WATERMARK_COLOR = Color.WHITE;
    private static final String DEFAULT_IMG_TYPE = "png";

    public static OutputStream addTextWatermark(WaterMarkImage waterMarkImage) {
        try {
            final String text = waterMarkImage.getText();
            final int fontSize = waterMarkImage.getFontSize() == 0 ? WATERMARK_FONT_SIZE : waterMarkImage.getFontSize();
            final Color color = waterMarkImage.getColor() == null ? WATERMARK_COLOR : waterMarkImage.getColor();
            final int width = waterMarkImage.getWidth();
            final int height = waterMarkImage.getHeight();
            final String type = waterMarkImage.getType() == null ? DEFAULT_IMG_TYPE : waterMarkImage.getType();
            final ByteArrayOutputStream output = ObjectUtils.defaultIfNull((ByteArrayOutputStream) waterMarkImage.getOutput(), new ByteArrayOutputStream());
            final InputStream inputStream = waterMarkImage.getInput();
            AssertUtil.nonNull(inputStream, "图片流不能为空");
            final BufferedImage read = ImageIO.read(inputStream);
            final Graphics2D graphics = (Graphics2D) read.getGraphics();
            final AlphaComposite instance = AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, .45f);
            graphics.setComposite(instance);
            graphics.setFont(new Font("微软雅黑", Font.BOLD, read.getHeight() / fontSize));
            graphics.setColor(color);
            graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

            int centerX = (read.getWidth() - graphics.getFontMetrics().stringWidth(text)) / 2;
            int centerY = read.getHeight() / 2;

            graphics.drawString(text, centerX, centerY);
            graphics.dispose();
            ImageIO.write(width == 0 ? read : resizeImage(read, width, height), type, output);
            return output;
        } catch (Exception e) {
            return null;
        }
    }

    private static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) {
        BufferedImage resizedImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = resizedImage.createGraphics();
        g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.drawImage(originalImage, 0, 0, targetWidth, targetHeight, null);
        g2d.dispose();
        return resizedImage;
    }
}
